package com.tg.service.suzakuEventBus.handler;

import com.google.common.eventbus.EventBus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by linzc on 2018/9/20.
 */
public class MqEventHandler extends EventBus implements InitializingBean {

    private static final Logger LOGGER = LoggerFactory.getLogger(MqEventHandler.class);

    private List<TaskEventHandler> handlers;

    public List<TaskEventHandler> getHandlers() {
        return handlers;
    }

    public void setHandlers(List<TaskEventHandler> handlers) {
        this.handlers = handlers;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        if (!CollectionUtils.isEmpty(this.handlers)) {
            this.handlers.forEach(this::register);
            String handlerNames = String.join("\\n->",
                    this.handlers.stream().map(TaskEventHandler::getBeanName).collect(Collectors.toList()));
            LOGGER.info("注册事件处理器：{}", handlerNames);
        } else {
            String msg = "事件处理器异常，缺少配置";
            LOGGER.warn(msg);
            throw new BeanCreationException(msg);
        }
    }


}
